package com.woniuxy.controller;

import com.woniuxy.Code.Status;
import com.woniuxy.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.woniuxy.service.UserService;
import com.woniuxy.utils.Result;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;
    @RequestMapping("/login")
    public Result login(@RequestParam("account") String account, String password) {
        // 调用UserService查询账号是否存在
        User user = userService.findUserByNameAndPwd(account,password);
        Result responseResult = new Result();
        //判断输入的User是否为空
        if (user != null) {
            // 找到user
            if (password.equals(user.getPassword())) {
                // 密码相同
                responseResult.setCode(200);
                responseResult.setMessage("登录成功");
                responseResult.setStatus(Status.LOGIN_SUCCESS);
                return responseResult;
            }
        }
        // 程序运行至此，表示登录失败
        responseResult.setCode(500);
        responseResult.setMessage("登录失败，用户名不存在或密码错误");
        responseResult.setStatus(Status.LOGIN_FALL);
        return responseResult;
    }
    @RequestMapping("/regist")
    public Result regist(String account, String pwd,String repwd, String email, String code, HttpServletRequest request) {
        // 返回类
        Result responseResult = new Result();

        // 1.从session中取出生成验证码时的字符串
        String oldCode = (String) request.getSession().getAttribute("code");
        // 2.对比验证码:忽略大小写
        if (!code.equalsIgnoreCase(oldCode)) {
            // 验证码不对
            responseResult.setCode(500);
            responseResult.setMessage("验证码错误");
            responseResult.setStatus(Status.FALL);
            return responseResult;
        }
        // 4.调service向数据库插入数据
        User user = new User();
        user.setAccount(account);
        user.setPassword(pwd);
        user.setEmail(email);
        if (userService.addUser(user) ) {
            // 注册成功
            responseResult.setCode(200);
            responseResult.setMessage("注册成功，激活邮件已发送");
            responseResult.setStatus(Status.SUCCESS);

        } else {
            // 注册失败
            responseResult.setCode(500);
            responseResult.setMessage("注册失败,请稍后再试");
            responseResult.setStatus(Status.FALL);
        }
        return responseResult;
    }

    public Result find(String account,String password) {
        Result<Object> responseResult = new Result<Object>();
        // 2.调方法
        if (userService.findUserByNameAndPwd(account,password) != null) {
            // 账号存在 账号不可用
            responseResult.setCode(500);
            responseResult.setStatus(Status.FALL);
            responseResult.setMessage("账号已存在");
        } else {
            // 账号可用
            responseResult.setCode(200);
            responseResult.setStatus(Status.SUCCESS);
            responseResult.setMessage("账号可用");
        }
        return responseResult;
    }


}
